package com.ruoyi.durian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.durian.domain.LlUserFriend;
import com.ruoyi.durian.service.LlUserFriendService;
import com.ruoyi.durian.mapper.LlUserFriendMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author lenovo
 * @description 针对表【ll_user_friend(个人中心模块-朋友表)】的数据库操作Service实现
 * @createDate 2024-07-26 16:17:16
 */
@Service
public class LlUserFriendServiceImpl extends ServiceImpl<LlUserFriendMapper, LlUserFriend>
        implements LlUserFriendService {

    @Resource
    private LlUserFriendMapper llUserFriendMapper;

    /**
     * 添加代叫乘客
     *
     * @return
     */
    @Override
    @Transactional
    public AjaxResult saveOrUpdateFriend(LlUserFriend llUserFriend) {
        Integer userId = SecurityUtils.getUserId().intValue();
        // 检查user是否是已经注册过的用户
        SysUser user = llUserFriendMapper.selectByFriendUserId(llUserFriend.getFriendPhone());
        if (user != null) {
            llUserFriend.setFriendUserId(Integer.parseInt(user.getUserId().toString()));
        }
        llUserFriend.setUpdateTime(new Date());
        llUserFriend.setUserId(Integer.parseInt(userId.toString()));
        llUserFriend.setCreateTime(new Date());
        saveOrUpdate(llUserFriend);
        return AjaxResult.success();
    }

    /**
     * 朋友列表
     *
     * @return
     */
    @Override
    public List<LlUserFriend> selectFriendList() {
        Long userId = SecurityUtils.getUserId();
        LambdaQueryWrapper<LlUserFriend> qw = new LambdaQueryWrapper<>();
        qw.eq(LlUserFriend::getUserId, userId);
        qw.orderByDesc(LlUserFriend::getCreateTime);
        return llUserFriendMapper.selectList(qw);
    }

    /**
     * 回显乘客代叫朋友信息
     *
     * @param id
     * @return
     */
    @Override
    public AjaxResult selectFriendById(Integer id) {
        return AjaxResult.success(llUserFriendMapper.selectById(id));
    }
}




